{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T04:08:14.496388Z",
     "start_time": "2025-02-23T04:08:09.378765Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "print(torch.__version__)\n",
    "\n",
    "import numpy as np"
   ],
   "id": "b27d5fbea2bf1ca1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.6.0+cu118\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 1.Tensor的理解：\n",
    "    1.常数，scaler:0阶张量\n",
    "    2.向量,vector,1阶张量\n",
    "    3.矩阵,matrix:2阶张量\n",
    "    4.3阶张量\n",
    "# 2. 创建张量的方法：\n",
    "     1.使用列表创建Tensor\n",
    "     2.使用numpy数组创建Tensor\n",
    "     3.通过torch的API创建Tensor\n",
    "     \n",
    "# 3. 张量的方法和属性\n",
    "    1.tensor.item(),当tensor中只有一个元素可以用的时候\n",
    "    2.Tensor转为ndarray\n",
    "    3.形状修改，tensor.view((3, 4)), 类似numpy中的reshape,是一种浅拷贝\n",
    "    4.获取维数、转置、轴滚动。\n",
    "    5.在方法后加_，会原地修改，相当于Tensorflow里的inplace"
   ],
   "id": "c8af6e2bb04d6f5e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T04:08:24.176382Z",
     "start_time": "2025-02-23T04:08:24.158040Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1.使用列表创建Tensor\n",
    "t1 = torch.Tensor([1, 2, 3])\n",
    "print(t1)\n",
    "\"\"\"\n",
    "tensor([1., 2., 3.])\n",
    "\"\"\"\n",
    "\n",
    "# 2.使用numpy数组创建Tensor\n",
    "array1 = np.arange(12).reshape(3, 4)\n",
    "t2 = torch.Tensor(array1)\n",
    "print(t2)\n",
    "\"\"\"\n",
    "tensor([[ 0.,  1.,  2.,  3.],\n",
    "        [ 4.,  5.,  6.,  7.],\n",
    "        [ 8.,  9., 10., 11.]])\n",
    "\"\"\"\n",
    "\n",
    "# 3.通过torch的API创建Tensor\n",
    "\"\"\" \n",
    "torch.empty(3,4)：创建3行四列的空的tensor,会用无用的数据进行填充\n",
    "torch.ones([3,4]):三行四列全为1的tensor\n",
    "torch.zeros([3,4]):三行四列全为0的tensor\n",
    "torch.rand([3,4]):三行四列随机值在[0,1]之间的值\n",
    "torch.randint(low = 0, high = 10, size = [3, 4]) 创建3*4的随机整数的Tensor，值区间：[low, high]\n",
    "torch.randn([3,4]) 均值为0，方差为1\n",
    "\"\"\"\n",
    "print(torch.empty(3,4))\n",
    "print(torch.ones([3,4]))\n",
    "print(torch.zeros([3,4]))\n",
    "print(torch.rand([3,4]))\n"
   ],
   "id": "729b851cb80003c5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1., 2., 3.])\n",
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n",
      "tensor([[6.4872e+29, 1.5050e-42, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]])\n",
      "tensor([[1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.]])\n",
      "tensor([[0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.]])\n",
      "tensor([[0.7118, 0.1447, 0.4625, 0.4258],\n",
      "        [0.8963, 0.1814, 0.0551, 0.3717],\n",
      "        [0.6575, 0.9403, 0.7490, 0.3818]])\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T04:13:56.542676Z",
     "start_time": "2025-02-23T04:13:56.537906Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 张量的方法和属性\n",
    "# 1.tensor.item(),当tensor中只有一个元素可以用的时候\n",
    "a =torch.tensor(np.arange(1))\n",
    "print(a)\n",
    "print(a.item())"
   ],
   "id": "ffd557fe6af13d51",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0], dtype=torch.int32)\n",
      "0\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T04:14:24.906414Z",
     "start_time": "2025-02-23T04:14:24.902398Z"
    }
   },
   "cell_type": "code",
   "source": "print(torch.Tensor([[[1]]]).item())",
   "id": "4959de06fe27780c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T04:14:53.362560Z",
     "start_time": "2025-02-23T04:14:53.358568Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 2.Tensor转为ndarray\n",
    "t2 = torch.Tensor([[[1,2]]])\n",
    "print(t2.numpy()) \n",
    "print(t2.shape)\n",
    "print(t2.size())\n",
    "print(t2.size())\n",
    "print(t2.size(-1))  #获取某个维度的数据"
   ],
   "id": "271bf1a1673701ab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1. 2.]]]\n",
      "torch.Size([1, 1, 2])\n",
      "torch.Size([1, 1, 2])\n",
      "torch.Size([1, 1, 2])\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0], dtype=torch.int32)\n",
      "0\n",
      "1.0\n",
      "[[[1. 2.]]]\n",
      "torch.Size([1, 1, 2])\n",
      "torch.Size([1, 1, 2])\n",
      "torch.Size([1, 1, 2])\n",
      "2\n",
      "torch.Size([1, 1, 2])\n",
      "tensor([[1., 2.]])\n",
      "tensor([1., 2.])\n",
      "tensor([[1.],\n",
      "        [2.]])\n",
      "3\n",
      "tensor(2.)\n",
      "tensor([[1, 2],\n",
      "        [3, 4],\n",
      "        [4, 6]])\n",
      "tensor([[[ 0,  1,  2,  3],\n",
      "         [ 4,  5,  6,  7],\n",
      "         [ 8,  9, 10, 11]],\n",
      "\n",
      "        [[12, 13, 14, 15],\n",
      "         [16, 17, 18, 19],\n",
      "         [20, 21, 22, 23]]], dtype=torch.int32)\n",
      "--------------------------------------------------\n",
      "tensor([[[ 0,  1,  2,  3],\n",
      "         [12, 13, 14, 15]],\n",
      "\n",
      "        [[ 4,  5,  6,  7],\n",
      "         [16, 17, 18, 19]],\n",
      "\n",
      "        [[ 8,  9, 10, 11],\n",
      "         [20, 21, 22, 23]]], dtype=torch.int32)\n",
      "--------------------------------------------------\n",
      "tensor([[[ 0,  1,  2,  3],\n",
      "         [12, 13, 14, 15]],\n",
      "\n",
      "        [[ 4,  5,  6,  7],\n",
      "         [16, 17, 18, 19]],\n",
      "\n",
      "        [[ 8,  9, 10, 11],\n",
      "         [20, 21, 22, 23]]], dtype=torch.int32)\n",
      "--------------------------------------------------\n",
      "tensor([[[ 0, 12],\n",
      "         [ 1, 13],\n",
      "         [ 2, 14],\n",
      "         [ 3, 15]],\n",
      "\n",
      "        [[ 4, 16],\n",
      "         [ 5, 17],\n",
      "         [ 6, 18],\n",
      "         [ 7, 19]],\n",
      "\n",
      "        [[ 8, 20],\n",
      "         [ 9, 21],\n",
      "         [10, 22],\n",
      "         [11, 23]]], dtype=torch.int32)\n",
      "--------------------------------------------------\n",
      "tensor([[[ 0, 12],\n",
      "         [ 4, 16],\n",
      "         [ 8, 20]],\n",
      "\n",
      "        [[ 1, 13],\n",
      "         [ 5, 17],\n",
      "         [ 9, 21]],\n",
      "\n",
      "        [[ 2, 14],\n",
      "         [ 6, 18],\n",
      "         [10, 22]],\n",
      "\n",
      "        [[ 3, 15],\n",
      "         [ 7, 19],\n",
      "         [11, 23]]], dtype=torch.int32)\n"
     ]
    }
   ],
   "execution_count": 3,
   "source": [
    "# 2.形状修改，tensor.view((3, 4)), 类似numpy中的reshape,是一种浅拷贝，仅仅形状发生改变,返回一个新的结果\n",
    "\n",
    "print(t2.size())\n",
    "print(t2.view([1,2]))\n",
    "print(t2.view([2]))\n",
    "print(t2.view([2, -1])) #\n",
    "\n",
    "#3. 获取维数\n",
    "print(t2.dim())\n",
    "\n",
    "#4.获取最大值\n",
    "print(t2.max())\n",
    "\n",
    "#5.转置\n",
    "t3 = torch.tensor([[1,3,4], [2,4,6]])\n",
    "print(t3.t())\n",
    "\n",
    "# 交换轴\n",
    "t4 = torch.tensor(np.arange(24).reshape(2,3,4))\n",
    "print(t4)\n",
    "print(\"-\"*50)\n",
    "print(t4.transpose(0,1))#交换0轴和1轴\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(1, 0, 2))#交换0轴和1轴\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(1, 2, 0))#交换0轴和1轴\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(2, 1, 0))#交换0轴和1轴\n"
   ],
   "id": "cf9560118db6403d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-23T04:36:19.272933Z",
     "start_time": "2025-02-23T04:36:19.268254Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#写一个ndarray\n",
    "array1 = np.array([[1,2,3],[4,5,6]])\n",
    "print(id(array1))\n",
    "array2=array1.reshape(3,2)\n",
    "print(id(array2))\n",
    "array2[0,0]=100\n",
    "print(array1)\n",
    "print(array2)"
   ],
   "id": "ed53c233260377be",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2109157364752\n",
      "2109158332176\n",
      "[[100   2   3]\n",
      " [  4   5   6]]\n",
      "[[100   2]\n",
      " [  3   4]\n",
      " [  5   6]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": "1+2",
   "id": "f4bf3bb2a424d274",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
